# Copyright 2014 Square Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.

require 'rails_helper'

RSpec.describe OccurrencesController, type: :controller do
  describe "#index" do
    before :all do
      @bug         = FactoryGirl.create(:bug)
      @occurrences = FactoryGirl.create_list(:rails_occurrence, 100, bug: @bug)
    end

    def sort(occurrences, reverse=false)
      occurrences.sort_by! { |b| [b.occurred_at, b.number] }
      occurrences.reverse! if reverse
      occurrences
    end

    it "should require a logged-in user" do
      get :index, polymorphic_params(@bug, true)
      expect(response).to redirect_to(login_url(next: request.fullpath))
    end

    context '[authenticated]' do
      before(:each) { login_as @bug.environment.project.owner }

      it_should_behave_like "action that 404s at appropriate times", :get, :index, 'polymorphic_params(@bug, true, format: "json")'

      context '[JSON]' do
        it "should load the first 50 occurrences" do
          get :index, polymorphic_params(@bug, true, format: 'json')
          expect(response.status).to eql(200)
          expect(JSON.parse(response.body).map { |r| r['number'] }).to eql(sort(@occurrences, true).map(&:number)[0, 50])
        end

        it "should load the first 50 occurrences ascending" do
          get :index, polymorphic_params(@bug, true, format: 'json', dir: 'asc')
          expect(response.status).to eql(200)
          expect(JSON.parse(response.body).map { |r| r['number'] }).to eql(sort(@occurrences).map(&:number)[0, 50])
        end

        it "should load the next 50 occurrences" do
          sort @occurrences, true
          get :index, polymorphic_params(@bug, true, format: 'json', last: @occurrences[49].number)
          expect(response.status).to eql(200)
          expect(JSON.parse(response.body).map { |r| r['number'] }).to eql(@occurrences.map(&:number)[50, 50])
        end
      end
    end
  end

  describe "#histogram" do
    before :all do
      @env = FactoryGirl.create(:environment)
      @bug = FactoryGirl.create(:bug, environment: @env)
    end

    it "should require a logged-in user" do
      get :histogram, polymorphic_params(@bug, true)
      expect(response).to redirect_to(login_url(next: request.fullpath))
    end

    context '[authenticated]' do
      before :all do
        occurrences = FactoryGirl.create_list(:rails_occurrence, 27, bug: @bug)
        occurrences.each_with_index { |occ, i| Occurrence.where(id: occ.id).update_all occurred_at: Time.at(1234567890) - (i*13).minutes }
        FactoryGirl.create(:deploy, environment: @env, revision: '30e7c2ff8758f4f19bfbc0a57e26c19ab69d1d44', deployed_at: Time.at(1234567890))
        FactoryGirl.create(:deploy, environment: @env, revision: '2dc20c984283bede1f45863b8f3b4dd9b5b554cc', deployed_at: Time.at(123467890) - 1.year)
      end

      before :each do
        login_as @bug.environment.project.owner
        allow(Time).to receive(:now).and_return(Time.at(1234567890)) # make sure we don't get boundary errors
      end

      it_should_behave_like "action that 404s at appropriate times", :get, :histogram, "polymorphic_params(@bug, true, dimensions: %w( host pid ), step: 1000*60*60*5, size: 20, format: 'json')"

      it "should return a histogram of occurrence frequencies and deploys" do
        get :histogram, polymorphic_params(@bug, true, dimensions: %w( host pid ), step: 1000*60*60*5, size: 20, format: 'json')
        expect(response.status).to eql(200)
        json = JSON.parse(response.body)
        expect(json['occurrences']).to eql([
                                                                [1234544400000, 1], [1234548000000, 5], [1234551600000, 4], [1234555200000, 5], [1234558800000, 4], [1234562400000, 5], [1234566000000, 3]
                                                            ])
        expect(json['deploys'].map { |d| d['revision'] }).to eql(%w(30e7c2ff8758f4f19bfbc0a57e26c19ab69d1d44))
      end

      it "should return empty arrays for bugs with no recent occurrences" do
        @bug.occurrences.delete_all
        get :histogram, polymorphic_params(@bug, true, dimensions: %w( host pid ), step: 1000*60*60*5, size: 20, format: 'json')
        expect(response.status).to eql(200)
        expect(JSON.parse(response.body)).to eql('occurrences' => [], 'deploys' => [])
      end
    end
  end

  describe "#aggregate" do
    before(:all) { @bug = FactoryGirl.create(:bug) }

    it "should require a logged-in user" do
      get :aggregate, polymorphic_params(@bug, true)
      expect(response).to redirect_to(login_url(next: request.fullpath))
    end

    context '[authenticated]' do
      before :all do
        occurrences_host1_pid1 = FactoryGirl.create_list(:rails_occurrence, 25, bug: @bug, host: 'host1', pid: 1)
        occurrences_host1_pid2 = FactoryGirl.create_list(:rails_occurrence, 25, bug: @bug, host: 'host1', pid: 2)
        occurrences_host2_pid1 = FactoryGirl.create_list(:rails_occurrence, 25, bug: @bug, host: 'host2', pid: 1)
        occurrences_host2_pid2 = FactoryGirl.create_list(:rails_occurrence, 25, bug: @bug, host: 'host2', pid: 2)

        # give them all unique occurred_at values
        occurrences            = occurrences_host1_pid1.zip(occurrences_host1_pid2).zip(occurrences_host2_pid1).zip(occurrences_host2_pid2).flatten
        occurrences.each_with_index { |o, i| Occurrence.where(id: o.id).update_all(occurred_at: Time.at(1234567890) - i.hours) }
      end

      before :each do
        login_as @bug.environment.project.owner
        allow(Time).to receive(:now).and_return(Time.at(1234567890)) # make sure we don't get boundary errors
      end

      it_should_behave_like "action that 404s at appropriate times", :get, :aggregate, "polymorphic_params(@bug, true, dimensions: %w( host pid ), step: 1000*60*60*5, size: 20, format: 'json')"

      it "should aggregate occurrences based on given dimensions" do
        get :aggregate, polymorphic_params(@bug, true, dimensions: %w( host pid ), step: 1000*60*60*5, size: 20, format: 'json')
        expect(response.status).to eql(200)
        expect(JSON.parse(response.body)).to eql({
                                                 "host" => [
                                                     {"label" => "host2", "data" => [[1234209600000, 100.0], [1234213200000, 100.0], [1234216800000, 0.0], [1234220400000, 0.0], [1234224000000, 100.0], [1234227600000, 100.0], [1234231200000, 0.0], [1234234800000, 0.0], [1234238400000, 100.0], [1234242000000, 100.0], [1234245600000, 0.0], [1234249200000, 0.0], [1234252800000, 100.0], [1234256400000, 100.0], [1234260000000, 0.0], [1234263600000, 0.0], [1234267200000, 100.0], [1234270800000, 100.0], [1234274400000, 0.0], [1234278000000, 0.0], [1234281600000, 100.0], [1234285200000, 100.0], [1234288800000, 0.0], [1234292400000, 0.0], [1234296000000, 100.0], [1234299600000, 100.0], [1234303200000, 0.0], [1234306800000, 0.0], [1234310400000, 100.0], [1234314000000, 100.0], [1234317600000, 0.0], [1234321200000, 0.0], [1234324800000, 100.0], [1234328400000, 100.0], [1234332000000, 0.0], [1234335600000, 0.0], [1234339200000, 100.0], [1234342800000, 100.0], [1234346400000, 0.0], [1234350000000, 0.0], [1234353600000, 100.0], [1234357200000, 100.0], [1234360800000, 0.0], [1234364400000, 0.0], [1234368000000, 100.0], [1234371600000, 100.0], [1234375200000, 0.0], [1234378800000, 0.0], [1234382400000, 100.0], [1234386000000, 100.0], [1234389600000, 0.0], [1234393200000, 0.0], [1234396800000, 100.0], [1234400400000, 100.0], [1234404000000, 0.0], [1234407600000, 0.0], [1234411200000, 100.0], [1234414800000, 100.0], [1234418400000, 0.0], [1234422000000, 0.0], [1234425600000, 100.0], [1234429200000, 100.0], [1234432800000, 0.0], [1234436400000, 0.0], [1234440000000, 100.0], [1234443600000, 100.0], [1234447200000, 0.0], [1234450800000, 0.0], [1234454400000, 100.0], [1234458000000, 100.0], [1234461600000, 0.0], [1234465200000, 0.0], [1234468800000, 100.0], [1234472400000, 100.0], [1234476000000, 0.0], [1234479600000, 0.0], [1234483200000, 100.0], [1234486800000, 100.0], [1234490400000, 0.0], [1234494000000, 0.0], [1234497600000, 100.0], [1234501200000, 100.0], [1234504800000, 0.0], [1234508400000, 0.0], [1234512000000, 100.0], [1234515600000, 100.0], [1234519200000, 0.0], [1234522800000, 0.0], [1234526400000, 100.0], [1234530000000, 100.0], [1234533600000, 0.0], [1234537200000, 0.0], [1234540800000, 100.0], [1234544400000, 100.0], [1234548000000, 0.0], [1234551600000, 0.0], [1234555200000, 100.0], [1234558800000, 100.0], [1234562400000, 0.0], [1234566000000, 0.0]]},
                                                     {"label" => "host1", "data" => [[1234209600000, 0.0], [1234213200000, 0.0], [1234216800000, 100.0], [1234220400000, 100.0], [1234224000000, 0.0], [1234227600000, 0.0], [1234231200000, 100.0], [1234234800000, 100.0], [1234238400000, 0.0], [1234242000000, 0.0], [1234245600000, 100.0], [1234249200000, 100.0], [1234252800000, 0.0], [1234256400000, 0.0], [1234260000000, 100.0], [1234263600000, 100.0], [1234267200000, 0.0], [1234270800000, 0.0], [1234274400000, 100.0], [1234278000000, 100.0], [1234281600000, 0.0], [1234285200000, 0.0], [1234288800000, 100.0], [1234292400000, 100.0], [1234296000000, 0.0], [1234299600000, 0.0], [1234303200000, 100.0], [1234306800000, 100.0], [1234310400000, 0.0], [1234314000000, 0.0], [1234317600000, 100.0], [1234321200000, 100.0], [1234324800000, 0.0], [1234328400000, 0.0], [1234332000000, 100.0], [1234335600000, 100.0], [1234339200000, 0.0], [1234342800000, 0.0], [1234346400000, 100.0], [1234350000000, 100.0], [1234353600000, 0.0], [1234357200000, 0.0], [1234360800000, 100.0], [1234364400000, 100.0], [1234368000000, 0.0], [1234371600000, 0.0], [1234375200000, 100.0], [1234378800000, 100.0], [1234382400000, 0.0], [1234386000000, 0.0], [1234389600000, 100.0], [1234393200000, 100.0], [1234396800000, 0.0], [1234400400000, 0.0], [1234404000000, 100.0], [1234407600000, 100.0], [1234411200000, 0.0], [1234414800000, 0.0], [1234418400000, 100.0], [1234422000000, 100.0], [1234425600000, 0.0], [1234429200000, 0.0], [1234432800000, 100.0], [1234436400000, 100.0], [1234440000000, 0.0], [1234443600000, 0.0], [1234447200000, 100.0], [1234450800000, 100.0], [1234454400000, 0.0], [1234458000000, 0.0], [1234461600000, 100.0], [1234465200000, 100.0], [1234468800000, 0.0], [1234472400000, 0.0], [1234476000000, 100.0], [1234479600000, 100.0], [1234483200000, 0.0], [1234486800000, 0.0], [1234490400000, 100.0], [1234494000000, 100.0], [1234497600000, 0.0], [1234501200000, 0.0], [1234504800000, 100.0], [1234508400000, 100.0], [1234512000000, 0.0], [1234515600000, 0.0], [1234519200000, 100.0], [1234522800000, 100.0], [1234526400000, 0.0], [1234530000000, 0.0], [1234533600000, 100.0], [1234537200000, 100.0], [1234540800000, 0.0], [1234544400000, 0.0], [1234548000000, 100.0], [1234551600000, 100.0], [1234555200000, 0.0], [1234558800000, 0.0], [1234562400000, 100.0], [1234566000000, 100.0]]}
                                                 ],
                                                 "pid"  => [
                                                     {"label" => 2, "data" => [[1234209600000, 100.0], [1234213200000, 0.0], [1234216800000, 100.0], [1234220400000, 0.0], [1234224000000, 100.0], [1234227600000, 0.0], [1234231200000, 100.0], [1234234800000, 0.0], [1234238400000, 100.0], [1234242000000, 0.0], [1234245600000, 100.0], [1234249200000, 0.0], [1234252800000, 100.0], [1234256400000, 0.0], [1234260000000, 100.0], [1234263600000, 0.0], [1234267200000, 100.0], [1234270800000, 0.0], [1234274400000, 100.0], [1234278000000, 0.0], [1234281600000, 100.0], [1234285200000, 0.0], [1234288800000, 100.0], [1234292400000, 0.0], [1234296000000, 100.0], [1234299600000, 0.0], [1234303200000, 100.0], [1234306800000, 0.0], [1234310400000, 100.0], [1234314000000, 0.0], [1234317600000, 100.0], [1234321200000, 0.0], [1234324800000, 100.0], [1234328400000, 0.0], [1234332000000, 100.0], [1234335600000, 0.0], [1234339200000, 100.0], [1234342800000, 0.0], [1234346400000, 100.0], [1234350000000, 0.0], [1234353600000, 100.0], [1234357200000, 0.0], [1234360800000, 100.0], [1234364400000, 0.0], [1234368000000, 100.0], [1234371600000, 0.0], [1234375200000, 100.0], [1234378800000, 0.0], [1234382400000, 100.0], [1234386000000, 0.0], [1234389600000, 100.0], [1234393200000, 0.0], [1234396800000, 100.0], [1234400400000, 0.0], [1234404000000, 100.0], [1234407600000, 0.0], [1234411200000, 100.0], [1234414800000, 0.0], [1234418400000, 100.0], [1234422000000, 0.0], [1234425600000, 100.0], [1234429200000, 0.0], [1234432800000, 100.0], [1234436400000, 0.0], [1234440000000, 100.0], [1234443600000, 0.0], [1234447200000, 100.0], [1234450800000, 0.0], [1234454400000, 100.0], [1234458000000, 0.0], [1234461600000, 100.0], [1234465200000, 0.0], [1234468800000, 100.0], [1234472400000, 0.0], [1234476000000, 100.0], [1234479600000, 0.0], [1234483200000, 100.0], [1234486800000, 0.0], [1234490400000, 100.0], [1234494000000, 0.0], [1234497600000, 100.0], [1234501200000, 0.0], [1234504800000, 100.0], [1234508400000, 0.0], [1234512000000, 100.0], [1234515600000, 0.0], [1234519200000, 100.0], [1234522800000, 0.0], [1234526400000, 100.0], [1234530000000, 0.0], [1234533600000, 100.0], [1234537200000, 0.0], [1234540800000, 100.0], [1234544400000, 0.0], [1234548000000, 100.0], [1234551600000, 0.0], [1234555200000, 100.0], [1234558800000, 0.0], [1234562400000, 100.0], [1234566000000, 0.0]]},
                                                     {"label" => 1, "data" => [[1234209600000, 0.0], [1234213200000, 100.0], [1234216800000, 0.0], [1234220400000, 100.0], [1234224000000, 0.0], [1234227600000, 100.0], [1234231200000, 0.0], [1234234800000, 100.0], [1234238400000, 0.0], [1234242000000, 100.0], [1234245600000, 0.0], [1234249200000, 100.0], [1234252800000, 0.0], [1234256400000, 100.0], [1234260000000, 0.0], [1234263600000, 100.0], [1234267200000, 0.0], [1234270800000, 100.0], [1234274400000, 0.0], [1234278000000, 100.0], [1234281600000, 0.0], [1234285200000, 100.0], [1234288800000, 0.0], [1234292400000, 100.0], [1234296000000, 0.0], [1234299600000, 100.0], [1234303200000, 0.0], [1234306800000, 100.0], [1234310400000, 0.0], [1234314000000, 100.0], [1234317600000, 0.0], [1234321200000, 100.0], [1234324800000, 0.0], [1234328400000, 100.0], [1234332000000, 0.0], [1234335600000, 100.0], [1234339200000, 0.0], [1234342800000, 100.0], [1234346400000, 0.0], [1234350000000, 100.0], [1234353600000, 0.0], [1234357200000, 100.0], [1234360800000, 0.0], [1234364400000, 100.0], [1234368000000, 0.0], [1234371600000, 100.0], [1234375200000, 0.0], [1234378800000, 100.0], [1234382400000, 0.0], [1234386000000, 100.0], [1234389600000, 0.0], [1234393200000, 100.0], [1234396800000, 0.0], [1234400400000, 100.0], [1234404000000, 0.0], [1234407600000, 100.0], [1234411200000, 0.0], [1234414800000, 100.0], [1234418400000, 0.0], [1234422000000, 100.0], [1234425600000, 0.0], [1234429200000, 100.0], [1234432800000, 0.0], [1234436400000, 100.0], [1234440000000, 0.0], [1234443600000, 100.0], [1234447200000, 0.0], [1234450800000, 100.0], [1234454400000, 0.0], [1234458000000, 100.0], [1234461600000, 0.0], [1234465200000, 100.0], [1234468800000, 0.0], [1234472400000, 100.0], [1234476000000, 0.0], [1234479600000, 100.0], [1234483200000, 0.0], [1234486800000, 100.0], [1234490400000, 0.0], [1234494000000, 100.0], [1234497600000, 0.0], [1234501200000, 100.0], [1234504800000, 0.0], [1234508400000, 100.0], [1234512000000, 0.0], [1234515600000, 100.0], [1234519200000, 0.0], [1234522800000, 100.0], [1234526400000, 0.0], [1234530000000, 100.0], [1234533600000, 0.0], [1234537200000, 100.0], [1234540800000, 0.0], [1234544400000, 100.0], [1234548000000, 0.0], [1234551600000, 100.0], [1234555200000, 0.0], [1234558800000, 100.0], [1234562400000, 0.0], [1234566000000, 100.0]]}
                                                 ]
                                             })
      end

      it "should collapse duplicate dimensions" do
        get :aggregate, polymorphic_params(@bug, true, dimensions: %w( host host pid ), step: 1000*60*60*5, size: 20, format: 'json')
        expect(JSON.parse(response.body)).to eql({
                                                 "host" => [
                                                     {"label" => "host2", "data" => [[1234209600000, 100.0], [1234213200000, 100.0], [1234216800000, 0.0], [1234220400000, 0.0], [1234224000000, 100.0], [1234227600000, 100.0], [1234231200000, 0.0], [1234234800000, 0.0], [1234238400000, 100.0], [1234242000000, 100.0], [1234245600000, 0.0], [1234249200000, 0.0], [1234252800000, 100.0], [1234256400000, 100.0], [1234260000000, 0.0], [1234263600000, 0.0], [1234267200000, 100.0], [1234270800000, 100.0], [1234274400000, 0.0], [1234278000000, 0.0], [1234281600000, 100.0], [1234285200000, 100.0], [1234288800000, 0.0], [1234292400000, 0.0], [1234296000000, 100.0], [1234299600000, 100.0], [1234303200000, 0.0], [1234306800000, 0.0], [1234310400000, 100.0], [1234314000000, 100.0], [1234317600000, 0.0], [1234321200000, 0.0], [1234324800000, 100.0], [1234328400000, 100.0], [1234332000000, 0.0], [1234335600000, 0.0], [1234339200000, 100.0], [1234342800000, 100.0], [1234346400000, 0.0], [1234350000000, 0.0], [1234353600000, 100.0], [1234357200000, 100.0], [1234360800000, 0.0], [1234364400000, 0.0], [1234368000000, 100.0], [1234371600000, 100.0], [1234375200000, 0.0], [1234378800000, 0.0], [1234382400000, 100.0], [1234386000000, 100.0], [1234389600000, 0.0], [1234393200000, 0.0], [1234396800000, 100.0], [1234400400000, 100.0], [1234404000000, 0.0], [1234407600000, 0.0], [1234411200000, 100.0], [1234414800000, 100.0], [1234418400000, 0.0], [1234422000000, 0.0], [1234425600000, 100.0], [1234429200000, 100.0], [1234432800000, 0.0], [1234436400000, 0.0], [1234440000000, 100.0], [1234443600000, 100.0], [1234447200000, 0.0], [1234450800000, 0.0], [1234454400000, 100.0], [1234458000000, 100.0], [1234461600000, 0.0], [1234465200000, 0.0], [1234468800000, 100.0], [1234472400000, 100.0], [1234476000000, 0.0], [1234479600000, 0.0], [1234483200000, 100.0], [1234486800000, 100.0], [1234490400000, 0.0], [1234494000000, 0.0], [1234497600000, 100.0], [1234501200000, 100.0], [1234504800000, 0.0], [1234508400000, 0.0], [1234512000000, 100.0], [1234515600000, 100.0], [1234519200000, 0.0], [1234522800000, 0.0], [1234526400000, 100.0], [1234530000000, 100.0], [1234533600000, 0.0], [1234537200000, 0.0], [1234540800000, 100.0], [1234544400000, 100.0], [1234548000000, 0.0], [1234551600000, 0.0], [1234555200000, 100.0], [1234558800000, 100.0], [1234562400000, 0.0], [1234566000000, 0.0]]},
                                                     {"label" => "host1", "data" => [[1234209600000, 0.0], [1234213200000, 0.0], [1234216800000, 100.0], [1234220400000, 100.0], [1234224000000, 0.0], [1234227600000, 0.0], [1234231200000, 100.0], [1234234800000, 100.0], [1234238400000, 0.0], [1234242000000, 0.0], [1234245600000, 100.0], [1234249200000, 100.0], [1234252800000, 0.0], [1234256400000, 0.0], [1234260000000, 100.0], [1234263600000, 100.0], [1234267200000, 0.0], [1234270800000, 0.0], [1234274400000, 100.0], [1234278000000, 100.0], [1234281600000, 0.0], [1234285200000, 0.0], [1234288800000, 100.0], [1234292400000, 100.0], [1234296000000, 0.0], [1234299600000, 0.0], [1234303200000, 100.0], [1234306800000, 100.0], [1234310400000, 0.0], [1234314000000, 0.0], [1234317600000, 100.0], [1234321200000, 100.0], [1234324800000, 0.0], [1234328400000, 0.0], [1234332000000, 100.0], [1234335600000, 100.0], [1234339200000, 0.0], [1234342800000, 0.0], [1234346400000, 100.0], [1234350000000, 100.0], [1234353600000, 0.0], [1234357200000, 0.0], [1234360800000, 100.0], [1234364400000, 100.0], [1234368000000, 0.0], [1234371600000, 0.0], [1234375200000, 100.0], [1234378800000, 100.0], [1234382400000, 0.0], [1234386000000, 0.0], [1234389600000, 100.0], [1234393200000, 100.0], [1234396800000, 0.0], [1234400400000, 0.0], [1234404000000, 100.0], [1234407600000, 100.0], [1234411200000, 0.0], [1234414800000, 0.0], [1234418400000, 100.0], [1234422000000, 100.0], [1234425600000, 0.0], [1234429200000, 0.0], [1234432800000, 100.0], [1234436400000, 100.0], [1234440000000, 0.0], [1234443600000, 0.0], [1234447200000, 100.0], [1234450800000, 100.0], [1234454400000, 0.0], [1234458000000, 0.0], [1234461600000, 100.0], [1234465200000, 100.0], [1234468800000, 0.0], [1234472400000, 0.0], [1234476000000, 100.0], [1234479600000, 100.0], [1234483200000, 0.0], [1234486800000, 0.0], [1234490400000, 100.0], [1234494000000, 100.0], [1234497600000, 0.0], [1234501200000, 0.0], [1234504800000, 100.0], [1234508400000, 100.0], [1234512000000, 0.0], [1234515600000, 0.0], [1234519200000, 100.0], [1234522800000, 100.0], [1234526400000, 0.0], [1234530000000, 0.0], [1234533600000, 100.0], [1234537200000, 100.0], [1234540800000, 0.0], [1234544400000, 0.0], [1234548000000, 100.0], [1234551600000, 100.0], [1234555200000, 0.0], [1234558800000, 0.0], [1234562400000, 100.0], [1234566000000, 100.0]]}
                                                 ],
                                                 "pid"  => [
                                                     {"label" => 2, "data" => [[1234209600000, 100.0], [1234213200000, 0.0], [1234216800000, 100.0], [1234220400000, 0.0], [1234224000000, 100.0], [1234227600000, 0.0], [1234231200000, 100.0], [1234234800000, 0.0], [1234238400000, 100.0], [1234242000000, 0.0], [1234245600000, 100.0], [1234249200000, 0.0], [1234252800000, 100.0], [1234256400000, 0.0], [1234260000000, 100.0], [1234263600000, 0.0], [1234267200000, 100.0], [1234270800000, 0.0], [1234274400000, 100.0], [1234278000000, 0.0], [1234281600000, 100.0], [1234285200000, 0.0], [1234288800000, 100.0], [1234292400000, 0.0], [1234296000000, 100.0], [1234299600000, 0.0], [1234303200000, 100.0], [1234306800000, 0.0], [1234310400000, 100.0], [1234314000000, 0.0], [1234317600000, 100.0], [1234321200000, 0.0], [1234324800000, 100.0], [1234328400000, 0.0], [1234332000000, 100.0], [1234335600000, 0.0], [1234339200000, 100.0], [1234342800000, 0.0], [1234346400000, 100.0], [1234350000000, 0.0], [1234353600000, 100.0], [1234357200000, 0.0], [1234360800000, 100.0], [1234364400000, 0.0], [1234368000000, 100.0], [1234371600000, 0.0], [1234375200000, 100.0], [1234378800000, 0.0], [1234382400000, 100.0], [1234386000000, 0.0], [1234389600000, 100.0], [1234393200000, 0.0], [1234396800000, 100.0], [1234400400000, 0.0], [1234404000000, 100.0], [1234407600000, 0.0], [1234411200000, 100.0], [1234414800000, 0.0], [1234418400000, 100.0], [1234422000000, 0.0], [1234425600000, 100.0], [1234429200000, 0.0], [1234432800000, 100.0], [1234436400000, 0.0], [1234440000000, 100.0], [1234443600000, 0.0], [1234447200000, 100.0], [1234450800000, 0.0], [1234454400000, 100.0], [1234458000000, 0.0], [1234461600000, 100.0], [1234465200000, 0.0], [1234468800000, 100.0], [1234472400000, 0.0], [1234476000000, 100.0], [1234479600000, 0.0], [1234483200000, 100.0], [1234486800000, 0.0], [1234490400000, 100.0], [1234494000000, 0.0], [1234497600000, 100.0], [1234501200000, 0.0], [1234504800000, 100.0], [1234508400000, 0.0], [1234512000000, 100.0], [1234515600000, 0.0], [1234519200000, 100.0], [1234522800000, 0.0], [1234526400000, 100.0], [1234530000000, 0.0], [1234533600000, 100.0], [1234537200000, 0.0], [1234540800000, 100.0], [1234544400000, 0.0], [1234548000000, 100.0], [1234551600000, 0.0], [1234555200000, 100.0], [1234558800000, 0.0], [1234562400000, 100.0], [1234566000000, 0.0]]},
                                                     {"label" => 1, "data" => [[1234209600000, 0.0], [1234213200000, 100.0], [1234216800000, 0.0], [1234220400000, 100.0], [1234224000000, 0.0], [1234227600000, 100.0], [1234231200000, 0.0], [1234234800000, 100.0], [1234238400000, 0.0], [1234242000000, 100.0], [1234245600000, 0.0], [1234249200000, 100.0], [1234252800000, 0.0], [1234256400000, 100.0], [1234260000000, 0.0], [1234263600000, 100.0], [1234267200000, 0.0], [1234270800000, 100.0], [1234274400000, 0.0], [1234278000000, 100.0], [1234281600000, 0.0], [1234285200000, 100.0], [1234288800000, 0.0], [1234292400000, 100.0], [1234296000000, 0.0], [1234299600000, 100.0], [1234303200000, 0.0], [1234306800000, 100.0], [1234310400000, 0.0], [1234314000000, 100.0], [1234317600000, 0.0], [1234321200000, 100.0], [1234324800000, 0.0], [1234328400000, 100.0], [1234332000000, 0.0], [1234335600000, 100.0], [1234339200000, 0.0], [1234342800000, 100.0], [1234346400000, 0.0], [1234350000000, 100.0], [1234353600000, 0.0], [1234357200000, 100.0], [1234360800000, 0.0], [1234364400000, 100.0], [1234368000000, 0.0], [1234371600000, 100.0], [1234375200000, 0.0], [1234378800000, 100.0], [1234382400000, 0.0], [1234386000000, 100.0], [1234389600000, 0.0], [1234393200000, 100.0], [1234396800000, 0.0], [1234400400000, 100.0], [1234404000000, 0.0], [1234407600000, 100.0], [1234411200000, 0.0], [1234414800000, 100.0], [1234418400000, 0.0], [1234422000000, 100.0], [1234425600000, 0.0], [1234429200000, 100.0], [1234432800000, 0.0], [1234436400000, 100.0], [1234440000000, 0.0], [1234443600000, 100.0], [1234447200000, 0.0], [1234450800000, 100.0], [1234454400000, 0.0], [1234458000000, 100.0], [1234461600000, 0.0], [1234465200000, 100.0], [1234468800000, 0.0], [1234472400000, 100.0], [1234476000000, 0.0], [1234479600000, 100.0], [1234483200000, 0.0], [1234486800000, 100.0], [1234490400000, 0.0], [1234494000000, 100.0], [1234497600000, 0.0], [1234501200000, 100.0], [1234504800000, 0.0], [1234508400000, 100.0], [1234512000000, 0.0], [1234515600000, 100.0], [1234519200000, 0.0], [1234522800000, 100.0], [1234526400000, 0.0], [1234530000000, 100.0], [1234533600000, 0.0], [1234537200000, 100.0], [1234540800000, 0.0], [1234544400000, 100.0], [1234548000000, 0.0], [1234551600000, 100.0], [1234555200000, 0.0], [1234558800000, 100.0], [1234562400000, 0.0], [1234566000000, 100.0]]}
                                                 ]
                                             })
      end

      it "should 422 for non-aggregating dimensions" do
        get :aggregate, polymorphic_params(@bug, true, dimensions: %w( host pid lat ), step: 1000*60*60*5, size: 20, format: 'json')
        expect(response.status).to eql(422)
      end

      it "should 422 for nonexistent dimensions" do
        get :aggregate, polymorphic_params(@bug, true, dimensions: %w( host pid madeup ), step: 1000*60*60*5, size: 20, format: 'json')
        expect(response.status).to eql(422)
      end

      it "should return an empty array given no dimensions" do
        get :aggregate, polymorphic_params(@bug, true, dimensions: [], step: 1000*60*60*5, size: 20, format: 'json')
        expect(response.status).to eql(200)
        expect(response.body).to eql('[]')
      end

      it "should 422 given too many dimensions" do
        get :aggregate, polymorphic_params(@bug, true, dimensions: %w( host pid client revision browser_os ), step: 1000*60*60*5, size: 20, format: 'json')
        expect(response.status).to eql(422)
      end
    end
  end

  describe "#show" do
    before(:all) { @occurrence = FactoryGirl.create(:rails_occurrence) }

    it "should require a logged-in user" do
      get :aggregate, polymorphic_params(@occurrence, false)
      expect(response).to redirect_to(login_url(next: request.fullpath))
    end

    context '[authenticated]' do
      before :each do
        login_as @occurrence.bug.environment.project.owner
      end

      it_should_behave_like "action that 404s at appropriate times", :get, :show, "polymorphic_params(@occurrence, false)"

      it "should not raise an exception for improperly-formatted JSON" do
        @occurrence.ivars = {foo: {bar: 'baz'}}
        expect { get :show, polymorphic_params(@occurrence, false) }.not_to raise_error
      end
    end
  end
end
